# -*- coding: utf-8 -*-
# PESCMS Login url接口请求库
#
# Created by: luoboss
#
# WARNING! All changes made in this file will be lost!
import http.cookiejar
import urllib.parse
import urllib.request

class urllib_page:
    
    def getUrl(self, url, data='', headers={"X-Requested-With": "XMLHttpRequest", "Accept": "application/json, text/javascript, */*; q=0.01"}, returnJson = False):
        if(data == ''):
            request = urllib.request.Request(url)
            for i in headers:
                request.add_header(i, headers[i])
        else:
            postData = data
            postData = urllib.parse.urlencode(postData).encode(encoding='UTF-8')
            request = urllib.request.Request(url, postData, headers)
        try:
            response = urllib.request.urlopen(request)
        except Exception as err:
            return False
        if(response.status != 200):
            return False
        else:
            if(returnJson == False):
                return response.read().decode('utf-8')
            else:
                import json
                try:
                    return json.loads(response.read().decode('utf-8'))
                except Exception as err:
                    return False
        
        
if __name__ == "__main__":
    url = urllib_page()
    print(url.getUrl(url="https://www.pescms.com"))
    
